package com.freight.base.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Address {


    public static void main(String[] args) {
        String areaPath = "E:\\code\\network_freight\\freight\\freight-base\\src\\main\\resources\\base_area.json";
        String cityPath = "E:\\code\\network_freight\\freight\\freight-base\\src\\main\\resources\\base_city.json";
        String provincePath = "E:\\code\\network_freight\\freight\\freight-base\\src\\main\\resources\\base_province.json";
        HashMap<String, List<AddressNode>> areaRes = getAreaMap(areaPath);
        HashMap<String, List<AddressNode>> cityRes = getCityMap(cityPath);
        List<AddressNode> procinceRes = getProcinceMap(provincePath);

        for (AddressNode procinceRe : procinceRes) {
            procinceRe.child = new ArrayList<>();
            AddressNode pall = new AddressNode();
            pall.setId(procinceRe.getId());
            pall.setPid(procinceRe.getId());
            pall.setLevel(2);
            pall.setName("不限");
            pall.child = new ArrayList<>();
            AddressNode call = new AddressNode();
            call.setId(procinceRe.getId());
            call.setPid(procinceRe.getId());
            call.setName("不限");
            call.setLevel(3);
            pall.child.add(call);
            procinceRe.child.add(pall);
            List<AddressNode> citys = cityRes.get(procinceRe.getId());
            for (AddressNode city : citys) {
                city.child = new ArrayList<>();
                List<AddressNode> areas = areaRes.get(city.getId());
                call = new AddressNode();
                call.setId(city.getId());
                call.setPid(city.getId());
                call.setName("不限");
                call.setLevel(3);
                city.child.add(call);
                if(areas != null) {
                    for (AddressNode area : areas) {
                        city.child.add(area);
                    }
                }

                procinceRe.child.add(city);
            }

        }
       System.out.println(JSONObject.toJSON(procinceRes));

    }


    public static HashMap<String, List<AddressNode>> getAreaMap(String areaPath) {
        String areaJson = readJsonFile(areaPath);
        JSONObject obj = JSONObject.parseObject(areaJson);
        JSONArray areas = obj.getJSONArray("RECORDS");
        HashMap<String, List<AddressNode>> city = new HashMap<>();
        for (int i = 0; i < areas.size(); i++) {
            JSONObject tmp = areas.getJSONObject(i);
            AddressNode tmpNode = new AddressNode();
            tmpNode.setId(tmp.getString("area_code"));
            tmpNode.setName(tmp.getString("area_name"));
            tmpNode.setPid(tmp.getString("city_code"));
            tmpNode.setLevel(3);
            if (city.get(tmp.getString("city_code")) == null) {
                List<AddressNode> res = new ArrayList<>();
                res.add(tmpNode);
                city.put(tmp.getString("city_code"), res);
            } else {
                city.get(tmp.getString("city_code")).add(tmpNode);
            }
        }
        return city;
    }

    public static HashMap<String, List<AddressNode>> getCityMap(String cityPath) {
        String areaJson = readJsonFile(cityPath);
        JSONObject obj = JSONObject.parseObject(areaJson);
        JSONArray areas = obj.getJSONArray("RECORDS");
        HashMap<String, List<AddressNode>> province = new HashMap<>();
        for (int i = 0; i < areas.size(); i++) {
            JSONObject tmp = areas.getJSONObject(i);
            AddressNode tmpNode = new AddressNode();
            tmpNode.setId(tmp.getString("city_code"));
            tmpNode.setName(tmp.getString("city_name"));
            tmpNode.setPid(tmp.getString("province_code"));
            tmpNode.setLevel(2);
            if (province.get(tmp.getString("province_code")) == null) {
                List<AddressNode> res = new ArrayList<>();
                res.add(tmpNode);
                province.put(tmp.getString("province_code"), res);
            } else {
                province.get(tmp.getString("province_code")).add(tmpNode);
            }
        }
        return province;
    }


    public static List<AddressNode> getProcinceMap(String provincePath) {
        String areaJson = readJsonFile(provincePath);
        JSONObject obj = JSONObject.parseObject(areaJson);
        JSONArray areas = obj.getJSONArray("RECORDS");
        List<AddressNode> china = new ArrayList<>();
        for (int i = 0; i < areas.size(); i++) {
            JSONObject tmp = areas.getJSONObject(i);
            AddressNode tmpNode = new AddressNode();
            tmpNode.setId(tmp.getString("province_code"));
            tmpNode.setName(tmp.getString("province_name"));
            tmpNode.setPid("100000");
            tmpNode.setLevel(1);
            china.add(tmpNode);
        }
        return china;
    }

    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
